package com.bonc.apicreator.ctrl;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bonc.apicreator.constant.BaseConstant;
import com.bonc.apicreator.dao.ProjectDao;
import com.bonc.apicreator.entity.Project;


/**
 * api文档controller
 *
 * @author gaopengyu
 * @version 2016年9月29日
 * @see DocumentController
 * @since
 */
@Controller
@RequestMapping("/document")
public class DocumentController {
    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(DocumentController.class);

    /**
     * projectDao
     */
    @Autowired
    private ProjectDao projectDao;

    /**
     * Description: 加载文档页面
     * 
     * @param session
     * @param model
     * @return String
     * @see
     */
    @RequestMapping(value = "")
    public String page(HttpSession session, Model model) {
        return "document/list.jsp";
    }

    /**
     * Description: ajax请求文档数据
     * 
     * @param pageable
     * @param condition
     * @param userId
     * @return Object
     * @see
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Pageable pageable, String condition, Long userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Project> docList;
        if (StringUtils.isEmpty(condition)) {
            docList = projectDao.findByCreateByOrderByCreateDateDesc(userId, pageable);
        }
        else {
            docList = projectDao.findByCreateByAndNameContainingOrderByCreateDateDesc(userId,
                condition, pageable);
        }
        map.put("list", docList.getContent());
        map.put("count", docList.getTotalElements());
        map.put("code", BaseConstant.STATE_SUCCESS);
        LOGGER.debug("list response:" + JSON.toJSONString(docList.getContent()));
        return JSON.toJSONString(map);
    }
}
